var IsoLine = {
	queryParams: {},
	saveParams: {},
	okSign: false,
	select2OptionInput: {
		dropdownCss: {
			'z-index': '100000',
			'font-size': '12px'
		},
		minimumResultsForSearch: -1
	},
	onSelectOpen: function() {
		var tmpThis = $(this);
		if (IsoLine.currOptionSelect) {
			var currId = IsoLine.currOptionSelect.attr('id');
			var thisId = tmpThis.attr('id');
			if (currId != thisId) {
				IsoLine.currOptionSelect.select2('close');
			}
		}
		IsoLine.currOptionSelect = tmpThis;
	},
	getConfigHtml: function(ele) {
		var target = $(ele);
		$.get(IsoLine.path + 'config.html', function(html) {
			var $html = $(html);
			var timeObjs = [];
			var ftArgs = $_doc.data('ftArgs');
			for (var i = 0; i < ftArgs.length; i++) {
				var obj = ftArgs[i];
				if (obj.argType == 'DAY' || obj.argType == 'DATETIME') {
					timeObjs.push(obj);
				}
			}
			var saveParams = IsoLine.saveParams;

			var beginTm = $html.find('#waterBeginTm');
			var endTm = $html.find('#waterEndTm');
			if (timeObjs.length == 0) {
				beginTm.append('<option value="noTime">未配置</option>');
				endTm.append('<option value="noTime">未配置</option>');
			} else {
				var beginTmVal = saveParams.beginTm;
				var endTmVal = saveParams.endTm;

				for (var i = 0; i < timeObjs.length; i++) {
					var line = timeObjs[i];
					var argName = line.argName;
					beginTm.append('<option ' + (argName == beginTmVal ? 'selected' : '') + ' value="' + line.showValue + '">' + argName + '</option>');
					endTm.append('<option ' + (argName == endTmVal ? 'selected' : '') + ' value="' + line.showValue + '">' + argName + '</option>');
				}
			}
			var waterTypeVal = saveParams.waterType;
			var waterAreaVal = saveParams.waterArea;
			$('#waterType option[value="' + waterTypeVal + '"]', $html).attr('selected', 'selected');
			$('#waterArea option[value="' + waterAreaVal + '"]', $html).attr('selected', 'selected');

			var allSelect = $html.find('.my_select');
			allSelect.select2(IsoLine.select2OptionInput).on('select2-open', IsoLine.onSelectOpen)

			target.html($html);
		})
	}
}

CKEDITOR.plugins.add('isoline', {
	icons: 'isoline',
	requires: 'widget',
	init: function(editor) {
		CKEDITOR.dialog.add('isoline', this.path + 'dialogs/isoline.js');
		IsoLine.path = this.path;
		var cls = 'isoline-preview';

		editor.widgets.add('isoline', {
			button: '等值线管理',
			dialog: 'isoline',
			draggable: false,
			requiredContent: 'div(isoline)',
			template: '<div class="isoline ' + cls + '"><div class="img-content"></div></div>',
			init: function() {
				this.once('ready', function() {
					if (this.data.isLoad == 1) {
						this.setData('isLoad', 0);
					}
				});
				// 打开窗口事件
				this.on('doubleclick', function(evt) {
					var element = this.element;
					var saveParams = JSON.parse(decodeURIComponent(element.data('save-params')));
					IsoLine.saveParams = saveParams;
				}, null, null, 5);
				// 关闭窗口
				this.on('data', function() {
					if (IsoLine.okSign) {
						IsoLine.okSign = false;
						var element = this.element;
						var target = element.$;
						$.get('isoLine/getLine', IsoLine.queryParams).done(function(data) {
							var imgContent = $('.img-content', target);
							imgContent.children().remove();
							imgContent.append('<img style="clear:both;display:block;margin:auto;" src="data:image/png;base64,' + data + '"/>');
						});
						element.data('save-params', encodeURIComponent(JSON.stringify(IsoLine.saveParams)));
					}
				});
			},
			upcast: function(el, data) {
				if (el.name == 'div' && el.hasClass(cls)) {
					// 标记此元素为页面加载的元素
					data.isLoad = 1;
					return el;
				}
			}
		});
	}
});